layui.use(['form', 'layedit', 'laydate','table'], function(){
    var table = layui.table;
    var form = layui.form
        ,layer = layui.layer
        ,layedit = layui.layedit
        ,laydate = layui.laydate;

    //日期
    var addStart = laydate.render({
        elem: '#add_coupon_startTime'
        ,max:"2099-12-31"
        ,done: function(value, date){
            addEnd.config.min ={
                year:date.year,
                month:date.month-1,
                date: date.date
            };
        }
    });
    var addEnd = laydate.render({
        elem: '#add_coupon_endTime'
        ,min:"1970-1-1"
        ,done: function (value, date) {
            addStart.config.max={
                year:date.year,
                month:date.month-1,
                date: date.date
            }
        }
    });

    var startTo = laydate.render({
        elem: '#coupon_startTime_to'
        ,max:"2099-12-31"
        ,done: function(value, date){
            startEnd.config.min ={
                year:date.year,
                month:date.month-1,
                date: date.date
            };
        }
    });
    var startEnd = laydate.render({
        elem: '#coupon_startTime_end'
        ,min:"1970-1-1"
        ,done: function (value, date) {
            startTo.config.max={
                year:date.year,
                month:date.month-1,
                date: date.date
            }
        }
    });

    var endTo = laydate.render({
        elem: '#coupon_endTime_to'
        ,max:"2099-12-31"
        ,done: function(value, date){
            endEnd.config.min ={
                year:date.year,
                month:date.month-1,
                date: date.date
            };
        }
    });
    var endEnd = laydate.render({
        elem: '#coupon_endTime_end'
        ,min:"1970-1-1"
        ,done: function (value, date) {
            endTo.config.max={
                year:date.year,
                month:date.month-1,
                date: date.date
            }
        }
    });


    //方法级渲染
    table.render({
        elem: '#coupon_table'
        ,url: '/couponTable'
        ,cols: [[
            {checkbox: true, fixed: true}
            ,{field:'id', title: 'ID', sort: true, fixed: true}
            ,{field:'name', title: '优惠券名称'}
            ,{field:'type', title: '优惠券类型', sort: true,templet: function(d){if(d.type == 1){return '定额'}else{return '满减'}}}
            ,{field:'startTime', title: '开始时间', templet: "<div>{{layui.util.toDateString(d.startTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"}
            ,{field:'endTime', title: '结束时间', templet: "<div>{{layui.util.toDateString(d.endTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"}
            ,{field:'totolAmount', title: '满减总金额', sort: true}
            ,{field:'discountAmount', title: '折扣金额', sort: true}
            ,{field:'amount', title: '数量'}
            ,{field:'status', title: '状态', sort: true,templet: "<div>{{(d.status == 1) ? '未过期':(d.status == 2)?'已过期':'已删除'}}</div>"}
        ]]
        ,id: 'coupon_table_render'
        ,page: true
        ,height: 310
        ,limit:5
        ,limits:[5,10]
    });

     active = {
        reload: function(){
            var name = $('#coupon_search');
            var startTime_to = $('#coupon_startTime_to');
            var startTime_end = $('#coupon_startTime_end');
            var endTime_to = $('#coupon_endTime_to');
            var endTime_end = $('#coupon_endTime_end');
            var type = $('#coupon_type');
            var status = $('#coupon_status');
            //执行重载
            table.reload('coupon_table_render', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                   couponName: name.val(),
                    startTimeTo:startTime_to.val(),
                    startTimeEnd:startTime_end.val(),
                    endTimeTo:endTime_to.val(),
                    endTimeEnd:endTime_end.val(),
                    type:type.val(),
                    status:status.val(),

                }
            }, 'data');
        }
    };

    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    form.on('submit(couponAdd)', function(data){
        // console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
        // console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
        // console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
        // return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        $.ajax({
            url:"/couponAdd",
            type:'post',
            dataType:'text',
            data:data.field,
            success:function (msg) {
                // console.log(msg)
                // console.log(msg == "SUCCESS")
                // console.log(msg == '"SUCCESS"')
                // console.log()
                if (msg == '"SUCCESS"'){
                    alert("新增成功")
                    layer.closeAll();
                    document.getElementById("coupon_install").reset();
                }
            }
        })

        return false;

    });

});

function couponAdd() {
    layui.use(['layer'], function () {
        let layer = layui.layer, $ = layui.$;
        layer.open({
            type: 1,
            anim: 2,
            area: ['400px', '500px'],
            title: '新增商品',
            shadeClose: false,
            content: $('#coupon_install')

        });
    });
}
